package org.fatewa.engine.exception;

import org.fatewa.engine.advice.protocol.RayquazaResponse;
import org.fatewa.engine.design.advice.AbstractExceptionProcessor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;

import java.util.Objects;

/**
 * 对接口层校验异常进行处理的异常处理器
 *
 * @author fatewa
 */
public class MethodArgumentNotValidProcessor extends AbstractExceptionProcessor<MethodArgumentNotValidException> {
    @Override
    public RayquazaResponse map(MethodArgumentNotValidException e) {
        String message = Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage();
        RayquazaResponse response = new RayquazaResponse();
        response.code(HttpStatus.BAD_REQUEST);
        response.msg(message);
        return response;
    }
}
